package net.oxygen.upload.controller;

import io.minio.UploadPartResponse;
import lombok.RequiredArgsConstructor;
import net.oxygen.upload.model.Chunk;
import net.oxygen.upload.model.vo.FileCheckResultVo;
import net.oxygen.upload.model.vo.FileChunkVo;
import net.oxygen.upload.request.CompleteMultipartUploadRequest;
import net.oxygen.upload.request.MultipartUploadCreateRequest;
import net.oxygen.upload.request.UploadPartRequest;
import net.oxygen.upload.response.FileUploadResponse;
import net.oxygen.upload.response.MultipartUploadCreateResponse;
import net.oxygen.upload.service.IFileService;
import net.oxygen.upload.util.JsonData;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * <p>
 * 功能：上传接口控制器
 * </p>
 *
 * @author 陈伟伟
 * @since 2023/12/19 9:56
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/upload")
@CrossOrigin
public class UploadController {

    private final IFileService fileService;

    /**
     * 普通小文件上传
     * @param file 文件对象
     * @return 上传结果
     */
    @PutMapping("/file")
    public JsonData uploadFile(@RequestPart MultipartFile file) {
        return fileService.uploadFile(file);
    }

    /**
     * 分片上传初始化
     * @param request 分片上传请求
     * @return 初始化结果，主要是上传的ID
     */
    @PostMapping("/multipart/create")
    public JsonData createMultipartUpload(@RequestBody MultipartUploadCreateRequest request) {
        MultipartUploadCreateResponse response = fileService.createMultipartUpload(request);
        return JsonData.success(response);
    }

    @GetMapping("/chunk")
    public JsonData checkChunk(Chunk chunk)
    {
        FileChunkVo FileCheckResultVo=fileService.checkchuck(chunk,false,"chenbo");
        return JsonData.success(FileCheckResultVo);
    }

//    @GetMapping("/getid")
//    public JsonData getid(Chunk chunk)
//    {
//        String  upid=fileService.getUploadId(chunk);
//        return JsonData.success(upid);
//    }
    @PostMapping("/chunk")
    public JsonData Chunk(Chunk chunk){
       if(chunk.getUpid()!=null) {
           fileService.uploadPart(chunk, chunk.getUpid());
       }
       chunk.setFile(null);
        return JsonData.success(chunk);
    }
    @PostMapping("/multipart/upload")
    public JsonData Upload(@RequestBody MultipartUploadCreateRequest request) {
        MultipartUploadCreateResponse response = fileService.createMultipartUpload(request);
        return JsonData.success(response);
    }

    /**
     * Uploads a part of a file using multipart upload.
     *
     * @param  request the upload part request
     * @return the JSON data containing the upload part response
     */
    @PutMapping(value = "/multipart/upload", consumes = "multipart/*", headers = "content-type=multipart/form-data", produces = "application/json;charset=UTF-8")
    public JsonData uploadPart(UploadPartRequest request) {
        fileService.uploadPart(request);
        return JsonData.success();
    }

    /**
     * 合并分片
     * @param request 合并请求
     * @return 合并结果，包含上传后的文件地址
     */
    @PostMapping("/multipart/complete")
    public JsonData completeMultipartUpload(@RequestBody @Validated CompleteMultipartUploadRequest request) {
        FileUploadResponse response = fileService.completeMultipartUpload(request);
        return JsonData.success(response);
    }

    @PostMapping("/multipartup/complete")
    public JsonData completeUpload(@RequestBody @Validated Chunk request) {
        FileUploadResponse response = fileService.completeMultipartUpload(request);
        return JsonData.success(response);
    }
}
